package top.haichi.webpaper.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.haichi.webpaper.entity.Message;
import top.haichi.webpaper.entity.Paper;
import top.haichi.webpaper.entity.Result;
import top.haichi.webpaper.entity.Student;
import top.haichi.webpaper.mapper.MessageMapper;
import top.haichi.webpaper.mapper.PaperMapper;
import top.haichi.webpaper.mapper.StudentMapper;
import top.haichi.webpaper.service.StudentService;
import top.haichi.webpaper.utils.FakeDataUtils;

import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
/**
 * 学生信息控制器
 */
public class StudentInformationController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private MessageMapper messageMapper;

    /**
     * 查询学生接口
     * @param student
     * @return
     */
    @PostMapping("/student/get")
    public Result getStudent(@RequestBody Student student) {
        return studentService.getStudent(student);
    }

//    /**
//     * 获取学生头像接口
//     * @param student
//     * @return
//     */
//    @PostMapping(value = "/student/portrait",produces = MediaType.IMAGE_JPEG_VALUE)
//    public byte[] getPortrait(@RequestBody Student student,HttpServletResponse response){
//
//        return studentService.getPortrait(student,response);
//    }

    /**
     * 更新学生信息
     * @param student
     * @return
     */
    @PostMapping("/student/update")
    public Result updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    /**
     * 学生查询论文
     * @param jsonNode
     * @return
     */
    @PostMapping("/student/getpapers")
    public Result getPaperByTeacher(@RequestBody JsonNode jsonNode){
        QueryWrapper<Paper> wrapper = new QueryWrapper<>();
        wrapper.eq("owner_id",jsonNode.get("id").asText())
                .like("title",jsonNode.get("keyWords").asText());

        List<Paper> papers = paperMapper.selectList(wrapper);
        return Result.success("获取成功",papers);
    }

    /**
     * 学生查询消息
     * @param student
     * @return
     */
    @PostMapping("/student/querymessage")
    public Result queryMessage(@RequestBody Student student){
        HashMap<String, Object> selectMap = new HashMap<>();
        selectMap.put("target",student.getId());
        List<Message> messages = messageMapper.selectByMap(selectMap);
//        HashMap<String, String> resultMap = new HashMap<>();
//        for (Message message : messages) {
//            resultMap.put()
//        }
        return Result.success("查询成功",messages);
    }

    /**
     * 修改信息状态
     * @param message
     * @return
     */
    @PostMapping("/student/updatemessage")
    public Result updateMessage(@RequestBody Message message){
        try{
            Message selectedMessage = messageMapper.selectById(message.getId());
            selectedMessage.setIsNew(false);
            messageMapper.updateById(selectedMessage);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("你干嘛，哈哈哎哟");
        }
        return Result.success("修改成功");
    }

    //fake
    @PostMapping("/student/fakepapers")
    public Result gererateFakePapers(@RequestBody Student student){
        String id = student.getId();
        for (Paper paper : FakeDataUtils.generatePapers(student.getId())) {
            paperMapper.insert(paper);
        }
        return Result.success("生成了四个假数据");
    }

}
